#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys, os
import db
from settings import config

def moveTo(src, dist):
    db.init_db_()
    session = db.create_session()
    for item in session.query(db.Fdfs).filter(db.Fdfs.content_type != 'd'):
        src_full = os.path.join(src, item.file_id.rsplit("/", 1)[1])
        if not src_full:
            print("[%s] not exists" % src_full)
            continue
        net_path = os.path.join(item.path, item.filename)
        local_path = os.path.join(dist, net_path.split("/", 1)[1])
        print(local_path)
        os.makedirs(os.path.dirname(local_path), exist_ok = True)
        with open(src_full, 'rb') as read:
            with open(local_path, 'wb') as write:
                write.write(read.read())


if __name__ == '__main__':
    moveTo('tmp/bak', 'tmp/bak1')